* File to create lags of weather variables at the zip-daily level


***Inputs:
* $Data/GameUser_zd.dta, clear
* $Data/user_activeisd_us_stations_Aug2019.dta
* $Data/DailyWeather_ISD`y'.dta
* $Data/DailyWeather_NEWISD`y'.dta
* $Data/DailyPrecipISD`y'


***Output:
* $Data/Lag`lag'dailyweather_zd.dta
* $Data/Lead`lead'dailyweather_zd.dta


forval lag = 1(1)7 {

use $Data/GameUser_zd.dta, clear

	rename date date_play

	gen date = date-`lag' 

	joinby zip using $Data/user_activeisd_us_stations_Aug2019.dta

   keep zip date* WBAN USAF lat longi LAT LON STATION *dist*

***2. MERGE WEATHER DATA FOR EACH HOUR AND LOCATION AND STATION***


	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyWeather_ISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	
	destring USAF, ignore("A") replace
 
	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyWeather_NEWISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	
	
	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyPrecipISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	 gen precipitation = precip_measurement > 0 

	
	

***3. GET CLOSEST WIND DIRECTION, CALCULATE THE WEIGHTED AVERAGE OF WEATHER VALUE BY ELEMENTTYPE AND ZIPCODE AND DATE AND HOUR***
	
	by zip date (isd_dist), sort: gen closest_isdwind = _n==1 if median_wind_direction <.
	gen closest_calm = calm if closest_isdwind==1
	gen median_closestwinddir = median_wind_direction if closest_isdwind == 1 
	
	
	
	
	gen weight = 1/isd_dist
	by zip date, sort: egen av_isd_dist =mean(isd_dist) 
	
	foreach var in temperature wind_speed calm precipitation {
	
		gen `var'_distance=`var'*weight

		by zip date, sort: egen sum_`var' = total(`var'_distance)
		by zip date, sort: egen sum_weight_`var' =  total(weight) if `var'_distance<.

		gen weighted_`var' =  sum_`var'/sum_weight_`var'
		

		}

	
	
	
	keep zip date* weighted_*  av_isd_dist  closest* median_closestwinddir


	duplicates drop

	keep if  weighted_temp <.  &  median_closestwinddir<.  // lose about half with aq 20 miles, isd at 30 miles
	
	replace weighted_temp = weighted_temp/10 // ISD temperature is scaled by 10 

	
	rename weighted_* lag`lag'weighted_* 
	rename closest_* lag`lag'closest_*
	rename median* lag`lag'median_*
	drop date
	
	rename date_play date
	
	save $Data/Lag`lag'dailyweather_zd.dta, replace
	
}
	
	
	
	
forval lead= 1(1)7 {

use $Data/GameUser_zd.dta, clear

	rename date date_play

	gen date = date+`lead' 

	joinby zip using $Data/user_activeisd_us_stations_Aug2019.dta

   keep zip date* WBAN USAF lat longi LAT LON STATION *dist*

***2. MERGE WEATHER DATA FOR EACH HOUR AND LOCATION AND STATION***


	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyWeather_ISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	
	destring USAF, ignore("A") replace
 
	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyWeather_NEWISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	
	
	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyPrecipISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	 gen precipitation = precip_measurement > 0 

	
	

***3. GET CLOSEST WIND DIRECTION, CALCULATE THE WEIGHTED AVERAGE OF WEATHER VALUE BY ELEMENTTYPE AND ZIPCODE AND DATE AND HOUR***
	
	by zip date (isd_dist), sort: gen closest_isdwind = _n==1 if median_wind_direction <.
	gen closest_calm = calm if closest_isdwind==1
	gen median_closestwinddir = median_wind_direction if closest_isdwind == 1 
	
	
	
	
	gen weight = 1/isd_dist
	by zip date, sort: egen av_isd_dist =mean(isd_dist) 
	
	foreach var in temperature wind_speed calm precipitation {
	
		gen `var'_distance=`var'*weight

		by zip date, sort: egen sum_`var' = total(`var'_distance)
		by zip date, sort: egen sum_weight_`var' =  total(weight) if `var'_distance<.

		gen weighted_`var' =  sum_`var'/sum_weight_`var'
		

		}

	
	
	
	keep zip date* weighted_*  av_isd_dist  closest* median_closestwinddir


	duplicates drop

	keep if  weighted_temp <.  &  median_closestwinddir<.  // lose about half with aq 20 miles, isd at 30 miles
	
	replace weighted_temp = weighted_temp/10 // ISD temperature is scaled by 10 

	
	rename weighted_* lead`lead'weighted_* 
	rename closest_* lead`lead'closest_*
	rename median* lead`lead'median_*
	drop date
	
	rename date_play date
	
	save $Data/Lead`lead'dailyweather_zd.dta, replace
	
	
}
	

	
